const { Random } = require('mockjs');
Random.extend({
    phonenumber: function(string) {
        var prefixs = new Array("139", "138", "137", "136", "135", "134", "159", "158", "157", "150", "151", "152", "188", "187", "182", "183", "184", "178", "130", "131", "132", "156", "155", "186", "185", "176", "133", "153", "189", "180", "181", "177")
        var phone = prefixs[parseInt(Math.random() * prefixs.length)]
        for (var i = 0; i < 8; i++) {
            phone = phone + Math.floor(Math.random() * 10)
        }
        return phone
    }
});


let List = []
import { param2Obj} from '../utils/urlutils'


import Mock from 'mockjs'
for(let i = 0 ;i<80;i++){
    List.push({
        id:Mock.mock('@increment()'),
        card: Mock.mock('@zip()'),
        viptype: '普通会员',
        rank:'VIP1',
     
      name: Mock.mock('@cname()'),
      number:Random.phonenumber(),
      consumption:Mock.mock('@float(100, 1000, 3, 2)'),
      balance:Mock.mock('@float(100, 1000, 3, 2)'),
      accrual:Mock.mock('@float(100, 1000, 3, 2)'),
      integral:'1000',
      opening:Mock.mock('@datetime()'),
      expiration:Mock.mock('@datetime()'),
       
    })
}








export default {
     
    getMemberData(config) {
        console.log('config',param2Obj(config.url));
        var {keyword,page,pageSize} = param2Obj(config.url);
        // console.log(keyword,page,pageSize);
        var res = List.filter(item=>{
            return item.name.indexOf(keyword)!=-1
        })        
        var resLimit = res.filter((item,index)=>{
            return (page-1)*pageSize<=index && index<page*pageSize;
        });
        return {
            code: 200,
            message: "获取数据成功",
            data:resLimit,
            total:List.length
            
        }
    },
    addMemberData(config){
        let {card,viptype,rank,name,number,consumption,balance,accrual,integral,opening,expiration} = JSON.parse(config.body)
        // console.log(config.body);
        List.unshift({
            
            card:card,
            viptype:'普通会员',
            rank:'VIP1',
            name:name,
            number:number,
            consumption:Mock.mock('@float(100, 1000, 3, 2)'),
            balance:Mock.mock('@float(100, 1000, 3, 2)'),
            accrual: Mock.mock('@float(100, 1000, 3, 2)'),
            integral:'1000',
            opening:Mock.mock('@datetime()'),
            expiration:Mock.mock('@datetime()')

        })
        
            
        return {
            code: 200,
            message: "addMemberData - 获取数据成功",
            data:List,
            
        }
    },
    editMemberData(config){
        let {id,card,viptype,rank,name,number,consumption,balance,accrual,integral,opening,expiration} = JSON.parse(config.body)

       List.forEach(item=>{
        if(item.id == id){
            item.card = card
            item.name = name
            item.number = number
            
            return
        }
       })
        return {
            code: 200,
            message: "editMemberData - 获取数据成功",
            data:List,
            
        }
    }
}



 
